﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using FriendsNearby.Services.Entities;
using System.Text;
using FriendsNearby.Services;

namespace FriendsNearby.Fbml
{
    public partial class FriendsNearbyList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

			FriendsNearbySystem friendsNearbySystem = new FriendsNearbySystem();
			IPeopleNearbyService peopleNearbyService = friendsNearbySystem.GetService<IPeopleNearbyService>();
			IUsersService userService = friendsNearbySystem.GetService<IUsersService>();

			UserRegistry user = userService.QueryUserRegistry(x => x.FacebookUserId == fbApplication.UserID);

            IEnumerable<PersonNearby> nearbyList = peopleNearbyService.GetContacts(user.UserID, QueryContactsKind.Nearby);
            StringBuilder nearbyFbmlListBuilder = new StringBuilder();
            nearbyFbmlListBuilder.AppendLine(string.Format("<b>Contacts in your nearby area</b> (closer than {0}) :", PersonNearbyPresentation.GetDistanceString(user.NearbyRadiusKM)));
            nearbyFbmlListBuilder.AppendLine("<table>");
            bool emptyNearbyList = true;
            foreach (PersonNearby personInfo in nearbyList)
            {
                emptyNearbyList = false;

                nearbyFbmlListBuilder.AppendLine("<tr>");
                nearbyFbmlListBuilder.AppendLine("<td>");

                string distance =  PersonNearbyPresentation.GetDistanceString(personInfo.Distance);
                if (!personInfo.IsHidden)
                {
                    nearbyFbmlListBuilder.AppendLine(string.Format("<fb:name uid=\"{0}\" /> at {1}", personInfo.FacebookUserID, distance));
                }
                else
                {
                    nearbyFbmlListBuilder.AppendLine(string.Format("{0} at {1}", personInfo.DisplayName, distance));
                }
                nearbyFbmlListBuilder.AppendLine("</td>");
                nearbyFbmlListBuilder.AppendLine("<td>");
                
                nearbyFbmlListBuilder.AppendLine("</td>");
                nearbyFbmlListBuilder.AppendLine("<td>");
                
                if (personInfo.CanRequestConnect)
                {
                    Uri requestContactUrl = new Uri(new Uri(FriendsNearby.ConfigHelper.ApplicationUrl), "Asp/RequestIntroduction.aspx");
					string requestContactArguments = string.Format("target_friendoffriend={0}", personInfo.UserID);

					nearbyFbmlListBuilder.AppendLine(string.Format("<a href=\"#\" clicktoshowdialog=\"request_intro_{0}\">(request introduction)</a>", personInfo.UserID));
					nearbyFbmlListBuilder.AppendLine(string.Format("<fb:dialog id=\"request_intro_{0}\">", personInfo.UserID));
                    nearbyFbmlListBuilder.AppendLine(string.Format("<fb:dialog-title>Request an introduction to {0}</fb:dialog-title>", personInfo.DisplayName));
                    nearbyFbmlListBuilder.AppendLine("<fb:dialog-content>");
                    nearbyFbmlListBuilder.AppendLine(string.Format("<fb:iframe src=\"{0}?{1}\" frameborder =\"0\" scrolling=\"no\" width=\"100%\" height=\"400\"/>", requestContactUrl, requestContactArguments));
                    nearbyFbmlListBuilder.AppendLine("</fb:dialog-content>");
                    nearbyFbmlListBuilder.AppendLine("</fb:dialog>");
                }

                nearbyFbmlListBuilder.AppendLine("</td>");
                nearbyFbmlListBuilder.AppendLine("</tr>");
            }
            if (emptyNearbyList)
            {
                nearbyFbmlListBuilder.AppendLine("<tr><td style=\"font-style:italic\">(there are no people in your nearby area)</td></tr>");
            }
            nearbyFbmlListBuilder.AppendLine("</table>");
            nearbyFbmlListBuilder.AppendLine("<br/><b><a href=\"#\" onclick=\"document.getElementById('friendNearbyList').setInnerFBML(friendlistwait);loadFriends();return false;\">Refresh list</a></b>");
            nearbyFbmlList = nearbyFbmlListBuilder.ToString();
        }

        private string nearbyFbmlList;
        public string NearbyFbmlList
        {
            get { return nearbyFbmlList; }
        }
    }
}
